﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HeresyManager.Comunication;

public partial class Controls_ComunicationControl : System.Web.UI.UserControl
{
    private int idCount;
    private Comunicator _comunicator;
    public Comunicator Comunicator
    {
        get { return _comunicator; }
        set { _comunicator = value; }
    }

    private Comunicator oldComunicator;



    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

    }

    protected void Page_Load(object sender, EventArgs e)
    {

        oldComunicator = Comunicator;

        Panel1.Controls.Clear();
        idCount = 0;
        this.ClearCachedClientID();
        AddControls(Comunicator);
    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if ( oldComunicator != Comunicator)
        {
            //foreach (var item in addedControl)
            //{
            //    //Panel1.Controls.Remove(item);
            //    //                               item.Visible=false;
            //}
            Panel1.Controls.Clear();
            idCount = 0;
            this.ClearCachedClientID();
            AddControls(Comunicator);

        }

    }

    private void AddControls(HeresyManager.Comunication.Comunicator comunicator)
    {
        if (comunicator == null)
        {
         

        }
        else if (comunicator is HeresyManager.Comunication.ChooseOne)
        {

            var uc = (ChooseOneControl)Page.LoadControl("~/Controls/ChooseOneControl.ascx");//path to control
            uc.ChooseOne = comunicator as HeresyManager.Comunication.ChooseOne;
            AddControl(uc);

        }
        else if (comunicator is HeresyManager.Comunication.EnterStringComunicator)
        {

            var uc = (Controls_EnterStringControl)Page.LoadControl("~/Controls/EnterStringControl.ascx");//path to control
            uc.EnterString = comunicator as HeresyManager.Comunication.EnterStringComunicator;
            AddControl(uc);
        }
        else if (comunicator is HeresyManager.Comunication.MultyComunicator)
        {
            foreach (var subComunicator in (comunicator as HeresyManager.Comunication.MultyComunicator).Comunicators)
            {
                AddControls(subComunicator);
            }
        }
        else if (comunicator is HeresyManager.Comunication.RoleDice)
        {
            var uc = (Controls_InputNumber)Page.LoadControl("~/Controls/InputNumber.ascx");//path to control
            uc.RoleDice = comunicator as HeresyManager.Comunication.RoleDice;
            AddControl(uc);
        }
        else
            throw new NotImplementedException();
    }

    private void AddControl(Control uc)
    {
        uc.ClientIDMode = System.Web.UI.ClientIDMode.Static;
        uc.ID = (idCount++).ToString();
        Panel1.Controls.Add(uc);//add control
    }


    protected override object SaveControlState()
    {
        return base.SaveControlState();
    }




}